
package com.joysuch.wwyt.dataimport.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.joysuch.wwyt.dataimport.bean.DataimportValidationResult;
import com.joysuch.wwyt.util.BeanValidator;
import net.sf.ehcache.hibernate.management.impl.BeanUtils;

public class DataImportValidator {

    public static DataimportValidationResult validate(Object obj) {
        Map<String, String> map = BeanValidator.validate(obj, new Class[0]);
        if (map.isEmpty()) {
            return null;
        }
        DataimportValidationResult result = new DataimportValidationResult();
        result.setErrors(map);
        try {
            int rowIndex = (int) BeanUtils.getBeanProperty(obj, "rowIndex");
            result.setRowIndex(rowIndex);
        } catch (Exception e) {
        }
        return result;
    }

    public static List<DataimportValidationResult> batchValidate(List list) {
        List<DataimportValidationResult> results = new ArrayList<DataimportValidationResult>(list.size());
        for (Object obj : list) {
            DataimportValidationResult r = validate(obj);
            if (r != null) {
                results.add(r);
            }
        }
        return results;
    }
}
